package com.feihong.txgw.tx_model;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.feihong.txgw.greendao.gen.ShopCarGoodsModelDao;
import com.feihong.txgw.inter.contract.product.IProListContract;
import com.feihong.txgw.json.goodsCart.GoodsCartParser;
import com.feihong.txgw.json.goodsCart.TerminalListParser;
import com.feihong.txgw.model.bean.shopcar.ShopcarOperateEntity;
import com.feihong.txgw.model.greendao.ShopCarGoodsModel;
import com.feihong.txgw.shopcar.bean.TerminalEntiyt;
import com.feihong.txgw.utils.greendao.GreenDaoOperateHelper;
import com.feihong.txgw.utils.helper.RxHelper;
import com.feihong.txgw.utils.manage.RetrofitManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName ProListMainModel
 * @package com.feihong.txgw.tx_model
 * @data 2018/5/8 12:40
 * @Describe
 * @Change
 */
public class ProListMainModel implements IProListContract.IProListMainModel {

    public static ProListMainModel create(){
        return new ProListMainModel();
    }
    @Override
    public Observable<TerminalListParser> getTerminalGoods(String pageSize, String storeID, String pageNum) {
        return RetrofitManager.getInstance().getShopCarRecommedGoods(pageSize, storeID, pageNum).compose(RxHelper.<TerminalListParser>rxSchedulerHelper());
    }

    @Override
    public Observable<GoodsCartParser> realAddGoodsToShopCar(List<ShopCarGoodsModel> goods) {
        List<ShopcarOperateEntity> mKeyContent = new ArrayList<>();
        Map<String,String> mParams = new HashMap<>();
        if (goods != null && goods.size() > 0) {
            for (ShopCarGoodsModel item : goods) {
                if(!TextUtils.isEmpty(item.getAction())){
                    ShopcarOperateEntity shopcarOperateEntity = new ShopcarOperateEntity();
                    shopcarOperateEntity.setAction(item.getAction());
                    shopcarOperateEntity.setNums(String.valueOf(item.getGoodsNum()));
                    shopcarOperateEntity.setSku_id(String.valueOf(item.getGoodsID()));
                    mKeyContent.add(shopcarOperateEntity);
                }
            }
        }
        mParams.put("content",JSON.toJSONString(mKeyContent));
        return RetrofitManager.getInstance().editShopCarGoodsToService(mParams).compose(RxHelper.<GoodsCartParser>rxSchedulerHelper());
    }

    @Override
    public Observable addGoodsToShopCar(TerminalEntiyt terminalEntiyt) {
        String goodsSkuID = terminalEntiyt.getSku_id();
        int goodsAllNum = Integer.valueOf(terminalEntiyt.getNumber());
        List<ShopCarGoodsModel> shopCarGoodsModels = GreenDaoOperateHelper.getModel(ShopCarGoodsModelDao.Properties.GoodsID, goodsSkuID);
        ShopCarGoodsModel tempModel = (shopCarGoodsModels != null && shopCarGoodsModels.size() != 0) ? shopCarGoodsModels.get(0) : null;
        if (tempModel != null && tempModel.getGoodsNum() >= goodsAllNum) {
            return Observable.just("此商品库存不足");
        }
        ShopCarGoodsModel model = new ShopCarGoodsModel();
        model.setGoodsAllNum(Integer.valueOf(terminalEntiyt.getNumber()));
        model.setGoodsName(terminalEntiyt.getSku_name());
        model.setGoodsPic(terminalEntiyt.getThumb());
        model.setGoodsNum(1);
        model.setGoodsSubtitle(terminalEntiyt.getSubtitle());
        model.setGoodsID(Integer.valueOf(terminalEntiyt.getSku_id()));
        model.setGoodsPrice(Float.valueOf(terminalEntiyt.getShop_price()));
        model.setIsUpdate(0);
        model.setAddTime(System.currentTimeMillis());
        model.setAction("add");
        model.setGoodsSpec(terminalEntiyt.getSpec());
        return Observable.just(model);
    }
}
